package com.stylefeng.guns.modular.netcar.service.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.modular.netcar.dao.VehicleInfMapper;
import com.stylefeng.guns.modular.netcar.model.DispatchPsnCar;
import com.stylefeng.guns.modular.netcar.model.DispatchPsnDriver;
import com.stylefeng.guns.modular.netcar.model.VehicleInf;
import com.stylefeng.guns.modular.netcar.service.IDispatchPsnCarService;
import com.stylefeng.guns.modular.netcar.service.IDispatchPsnDriverService;
import com.stylefeng.guns.modular.netcar.service.IVehicleInfService;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.stylefeng.guns.modular.system.model.Org;
import com.stylefeng.guns.modular.system.service.IOrgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**

 * 车辆信息 Service 实现 
 * @author sasazaixian
 * @Date 2018-10-23 11:06
 */
@Service
public class VehicleInfServiceImpl extends ServiceImpl<VehicleInfMapper,VehicleInf> implements IVehicleInfService {

    @Resource
    private VehicleInfMapper vehicleInfMapper;
    @Autowired
    private IDispatchPsnDriverService dispatchPsnDriverService;
    @Autowired
    private IDispatchPsnCarService dispatchPsnCarService;
    @Autowired
    private IOrgService orgService;

    /**
     * 删除车辆信息
     * @param vehicleInf
     * @return     
     */
    @Transactional
    public int deleteVehicleInf(VehicleInf vehicleInf,String token)throws Exception{
        if(vehicleInf==null ){
            throw new RuntimeException("参数不全！");
        }
        List<VehicleInf> listVehicleInf=vehicleInfMapper.selectListVehicleInf(vehicleInf);
        for(VehicleInf en:listVehicleInf){
            DispatchPsnDriver dispatchPsnDriver=new DispatchPsnDriver();
            dispatchPsnDriver.setViId(en.getViId());
            List list=dispatchPsnDriverService.selectListDispatchPsnDriver(dispatchPsnDriver,token);
            if(list!=null && list.size()>0){
                throw new RuntimeException("已经在调度指派驾驶员中使用，不能删除！");
            }

            DispatchPsnCar dispatchPsnCar=new DispatchPsnCar();
            dispatchPsnCar.setViId(en.getViId());
            list=dispatchPsnCarService.selectListDispatchPsnCar(dispatchPsnCar,token);
            if(list!=null && list.size()>0){
                throw new RuntimeException("已经在调度指派车辆中使用，不能删除！");
            }
        }
        return this.vehicleInfMapper.deleteVehicleInf(vehicleInf);
    }
    /**
     * 批量删除车辆信息
     * @param ids 主键ID数组
     * @return 
     */
    @Transactional
    public int deleteBatchVehicleInf(String[] ids,String token)throws Exception{
        for(String id:ids){

            DispatchPsnDriver dispatchPsnDriver=new DispatchPsnDriver();
            dispatchPsnDriver.setViId(id);
            List list=dispatchPsnDriverService.selectListDispatchPsnDriver(dispatchPsnDriver,token);
            if(list!=null && list.size()>0){
                throw new RuntimeException("已经在调度指派驾驶员中使用，不能删除！");
            }

            DispatchPsnCar dispatchPsnCar=new DispatchPsnCar();
            dispatchPsnCar.setViId(id);
            list=dispatchPsnCarService.selectListDispatchPsnCar(dispatchPsnCar,token);
            if(list!=null && list.size()>0){
                throw new RuntimeException("已经在调度指派车辆中使用，不能删除！");
            }
        }
        return this.vehicleInfMapper.deleteBatchVehicleInf(ids);
    }
    /**
     * 查询车辆信息列表
     * @param vehicleInf 车辆信息对象
     * @return
     */
    public List<VehicleInf> selectListVehicleInf(VehicleInf vehicleInf,String token)throws Exception{
        return  this.vehicleInfMapper.selectListVehicleInf(vehicleInf);
    }
    /**
	 * 分页查询车辆信息列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListVehicleInf(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(vehicleInfMapper.selectListVehicleInf(page,(VehicleInf)pager.getObj()));
        List<VehicleInf> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param vehicleInf 车辆信息对象
     * @return 
     */
    public  List<VehicleInf> selectListVehicleInfForUpdate(VehicleInf vehicleInf)throws Exception{
        return  this.vehicleInfMapper.selectListVehicleInfForUpdate(vehicleInf);
    }
    /**
     * 通过主键id查询的车辆信息列表
     * @param id 主键id
     * @return     
     */
    public  VehicleInf findVehicleInfById(String id,String token)throws Exception{
        return  this.vehicleInfMapper.findVehicleInfById(id);
    }

    /**
     * 新增车辆信息数据
     * @param vehicleInf  车辆信息对象
     * @return
     */
    @Transactional
    public int insertVehicleInf(VehicleInf vehicleInf,String token) throws Exception { 
        if(vehicleInf!=null && (vehicleInf.getViId()==null || vehicleInf.getViId().trim().length()<10)){
            vehicleInf.setViId(IdWorker.get32UUID());
        }
        if(vehicleInf.getViDeptId()==null || vehicleInf.getViDeptId().trim().length()<1){
            throw new RuntimeException("所属车队不能为空");
        }
        //根据所属车队找到所属机构ID
        String deptId=vehicleInf.getViDeptId();
        Org org=new Org();
        org.setSoId(Integer.parseInt(deptId));
        org=orgService.selectOne(new EntityWrapper<>(org));
        if(org==null ){
            throw new RuntimeException("所属车队不存在！");
        }
        if(org.getSoType().equals("1")){
            vehicleInf.setSoId(deptId);
        }else{
            Integer pid=org.getSoPid();
            BB:
            while(true){
                if(pid!=null){
                    Org porg=this.orgService.selectById(pid);
                    if(porg!=null){
                        if(StringUtils.equals("1",porg.getSoType())){
                            vehicleInf.setSoId(""+porg.getSoId());
                            break;
                        }else{
                            pid= porg.getSoPid();
                            continue BB;
                        }
                    }
                }
                break;
            }
        }
        return  this.vehicleInfMapper.insertVehicleInf(vehicleInf);
    }
    
    /**
     * 更新车辆信息数据
     * @param vehicleInf 车辆信息对象
     * @return     
     */
    @Transactional
    public int updateVehicleInf(VehicleInf vehicleInf,String token) throws Exception {
        if(vehicleInf.getViDeptId()!=null && vehicleInf.getSoId().trim().length()>0){
            //根据所属车队找到所属机构ID
            String deptId=vehicleInf.getViDeptId();
            Org org=new Org();
            org.setSoId(Integer.parseInt(deptId));
            org=orgService.selectOne(new EntityWrapper<>(org));
            if(org==null ){
                throw new RuntimeException("所属车队不存在！");
            }
            if(org.getSoType().equals("1")){
                vehicleInf.setSoId(deptId);
            }else{
                Integer pid=org.getSoPid();
                BB:
                while(true){
                    if(pid!=null){
                        Org porg=this.orgService.selectById(pid);
                        if(porg!=null){
                            if(StringUtils.equals("1",porg.getSoType())){
                                vehicleInf.setSoId(""+porg.getSoId());
                                break;
                            }else{
                                pid= porg.getSoPid();
                                continue BB;
                            }
                        }
                    }
                    break;
                }
            }
        }
        return  this.vehicleInfMapper.updateVehicleInf(vehicleInf);
    }    
    /**
     * 查询车辆信息返回key-Value属性
     * @param vehicleInf 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsVehicleInf(VehicleInf vehicleInf,String token) throws Exception {
        List<VehicleInf> listVehicleInf=this.vehicleInfMapper.selectListVehicleInf(vehicleInf);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(VehicleInf item:listVehicleInf){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getViId());
            opts.setValue(item.getViVehiclePlate());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
